package tkaczyk.simtalk.pc;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.InputMap;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;

import tkaczyk.simtalk.messenger.Messenger;

@SuppressWarnings("serial")
public class SenderPane extends JTextPane implements KeyListener {
	private Messenger messenger;
	private ContentPane contentPane;

	public SenderPane() {
		super();

		InputMap map = this.getInputMap();
		KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
		KeyStroke shiftEnterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK);
		map.put(enterKey, "none");
		map.put(shiftEnterKey, "insert-break");

		addKeyListener(this);
	}

	public void keyPressed(KeyEvent key) {
	}

	public void keyReleased(KeyEvent key) {
		if (key.getKeyCode() == KeyEvent.VK_ENTER && key.getModifiers() == 0) {
			messenger.setNick(contentPane.getNick());
			messenger.SendMessage(getText());
			setText("");
		}
	}

	public void keyTyped(KeyEvent key) {
	}

	public void setMessenger(Messenger messenger) {
		this.messenger = messenger;
	}
	
	public void setContentPane(ContentPane contentPane) {
		this.contentPane = contentPane;
	}
}
